<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author hancing
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'dp:kecheng:dpKecheng:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="80%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" >
      <template #kcdetail="{ model, field }">
        <MarkDown
          ref="markdownRef"
          v-model:value="model[field]"
          :bizKey="record.id"
          :bizType="'testDataChild_' + field"
          :height="400"
        />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsDpKechengDpKechengForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { DpKecheng, dpKechengSave, dpKechengForm } from '/@/api/dp/kecheng/dpKecheng';
  import {  dpTypeTreeData } from '/@/api/dp/dptype/dpType';

  import { WangEditor } from '/@/components/WangEditor';
  import { MarkDown } from '/@/components/Markdown';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('dp.kecheng.dpKecheng');
  const { showMessage } = useMessage();
  const record = ref<DpKecheng>({} as DpKecheng);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增课程') : t('编辑课程'),
  }));
  const markdownRef=ref();

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('分类'),
      field: 'kctype',
      fieldLabel: 'typename',
      component: 'TreeSelect',
      componentProps: {
        allowClear: true,
        canSelectParent: false,
        
      },
     rules: [{ required: true }],
    },
    
    {
      
      label: t('封面图片'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'dpKecheng_image',
        uploadType: 'image',
      },
      
    },
     {
      label: t('标题'),
      field: 'kctitle',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      colProps: { lg: 24, md: 24 },
       required: true,
    },
    {
      label: t('简介'),
      field: 'kcjianjie',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
       required: true,
    },
    {
      label: t('课程状态'),
      field: 'kcstatus',
      component: 'Select',
      componentProps: {
        dictType: 'dp_kecheng_status',
        allowClear: true,
      },
      required: true,
    },
    
    {
      label: t('标签'),
      field: 'tags',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('售出基数'),
      field: 'basesell',
      component: 'Input',
      rules: [{ required: true }, { pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('价格'),
      field: 'price',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      
      rules: [{ required: true },{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
   {
      label: t('下载地址'),
      field: 'baiduurl',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
       colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('课程详情'),
      field: 'kcdetail',
      component: 'InputTextArea',
      slot: 'kcdetail',
      colProps: { lg: 24, md: 24 },
    },
    
    
    
  ];

  const [registerForm, { resetFields, setFieldsValue, validate ,updateSchema}] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await dpKechengForm(data);
    record.value = (res.dpKecheng || {}) as DpKecheng;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);

    updateSchema([
      {
        field: 'kctype',
        componentProps: {
          api: dpTypeTreeData,
          params: {
            excludeCode: record.value.id,
            isShowRawName: true,
          },
        },
      }
    ]);

    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await dpKechengSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
